import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter2_demo/layout_demo/ListView.dart';
import 'package:flutter2_demo/layout_demo/ListView2.dart';
import 'package:flutter2_demo/layout_demo/alertAialog.dart';
import 'package:flutter2_demo/layout_demo/bottomBar_default.dart';
import 'package:flutter2_demo/layout_demo/bottomBar_diy.dart';
import 'package:flutter2_demo/layout_demo/drawer.dart';
import 'package:flutter2_demo/layout_demo/gridView.dart';
import 'package:flutter2_demo/layout_demo/layout.dart';
import 'package:flutter2_demo/layout_demo/tabBar.dart';
import 'package:flutter2_demo/navigator_demo/page1.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: MaterialApp(
        title: 'app标题',

        theme: ThemeData(
          primarySwatch: Colors.pink,
        ),
        //主题色
        debugShowCheckedModeBanner: false,
        //控制右上角debug标签

        // home: Page1(),// home: MyHomePage(),
        //!使用route配置路由，需要删除home
        routes: {
          "/": (context) => HomePage(),
          "layout": (context) => LayoutPage(),
          "page1": (context) => Page1(),
          "tabDemo1": (context) => TabDemo1(),
          "drawerDemo1": (context) => DrawerDemo1(),
          "bottomBarDemo1": (context) => BottomBarDemo1(), //默认底部选项卡
          "bottomBarDemo2": (context) => BottomBarDemo2(), //自定义底部选项卡
          "listViewDemo1": (context) => ListViewDemo1(), //列表练习1
          "listViewDemo2": (context) => ListViewDemo2(), //列表练习2
          "gridViewDemo1": (context) => GridViewDemo1(), //网格布局练习1
          "alertDialogDemo1": (context) => AlertDialogDemo1(), //弹窗练习1
        },
        initialRoute: "/", //初始化页面
        // onGenerateRoute: (s) {
        //   //不监听已经注册路由表
        //   print(s);
        // },
      ),
    );
  }
}

class HomePage extends StatefulWidget {
  List<Widget> _tabsView = [Tab1(), Tab2(), Tab3()];

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage>
    with SingleTickerProviderStateMixin {
  List _tabs = ['组件', 'Flutter', 'IOS'];
  late TabController tabController;

  @override
  void initState() {
    // TODO: implement initState
    //页面创建的时候新建
    super.initState();
    tabController = new TabController(length: _tabs.length, vsync: this);
    tabController.addListener(() {
      setState(() {
        print('tab切换');
      });
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    //  页面退出销毁Controller，避免内存溢出
    super.dispose();
    tabController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('首页'),
        centerTitle: true,
        //文字居中
        elevation: 10,
        //阴影
        leading: IconButton(
          icon: Icon(Icons.home),
          onPressed: () {
            print('点击了homeIcon');
          },
        ),
        //左侧按钮
        actions: [
          IconButton(
            icon: Icon(Icons.add),
            onPressed: () {
              print('点击了add');
            },
          ),
          IconButton(
            icon: Icon(Icons.search),
            onPressed: () {
              print('点击了search');
            },
          )
        ],
        bottom: TabBar(
          controller: tabController,
          tabs: _tabs
              .map((e) => Tab(
                    text: e,
                  ))
              .toList(),
        ),
      ),
      // body: widget._tabsView[tabIndex],
      body: TabBarView(
        children: widget._tabsView,
        controller: tabController,
      ),

      //  Center(
      //         child: Column(
      //           children: [
      //             ElevatedButton(
      //                 onPressed: () {
      //                   Navigator.of(context).pushNamed('layout');
      //                 },
      //                 child: Text(tabIndex.toString()))
      //           ],
      //         ),
      //       )
    );
  }
}

class Tab1 extends StatelessWidget {
  const Tab1({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        ElevatedButton(
            //跳转tabs组件
            onPressed: () {
              Navigator.of(context).pushNamed('tabDemo1');
            },
            child: Text('tabs')),
        ElevatedButton(
            //跳转tabs组件
            onPressed: () {
              Navigator.of(context).pushNamed('drawerDemo1');
            },
            child: Text('侧边抽屉')),
        ElevatedButton(
            //跳转tabs组件
            onPressed: () {
              Navigator.of(context).pushNamed('bottomBarDemo1');
            },
            child: Text('默认底部选项卡')),
        ElevatedButton(
            onPressed: () {
              Navigator.of(context).pushNamed('bottomBarDemo2');
            },
            child: Text('自定义底部选项卡')),
        ElevatedButton(
            onPressed: () {
              Navigator.of(context).pushNamed('listViewDemo1');
            },
            child: Text('列表练习1')),
        ElevatedButton(
            onPressed: () {
              Navigator.of(context).pushNamed('listViewDemo2');
            },
            child: Text('列表练习2')),
        ElevatedButton(
            onPressed: () {
              Navigator.of(context).pushNamed('gridViewDemo1');
            },
            child: Text('网格布局')),
        ElevatedButton(
            onPressed: () {
              Navigator.of(context).pushNamed('alertDialogDemo1');
            },
            child: Text('弹窗练习')),
        
      ],
    );
  }
}

class Tab2 extends StatelessWidget {
  const Tab2({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text('222222'),
    );
  }
}

class Tab3 extends StatelessWidget {
  const Tab3({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text('333333'),
    );
  }
}
